package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/ydd_advimg")
public class AdvimgController {

    @Autowired
    private AdvimgService advimgService;
    @Autowired
    private AdvposService advposService;

    @GetMapping("/list")
    public ResultData list(String advimgdesc, String advimgpos, int page, int limit) {
        //分页构造器
        IPage<Advimg> pageInfo = new Page<>(page, limit);
        //条件构造器
        LambdaQueryWrapper<Advimg> queryWrapper = new LambdaQueryWrapper<>();


        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(advimgdesc), Advimg::getAdvimgdesc, advimgdesc)
                .like(StringUtils.isNotEmpty(advimgpos), Advimg::getAdvimgpos, advimgpos);
        //添加排序字段
        queryWrapper.orderByDesc(Advimg::getId);

        advimgService.page(pageInfo, queryWrapper);


        pageInfo.getRecords().stream().map((item) -> {
            String advopsnameId = item.getAdvimgpos();
            Advpos byId = advposService.getById(advopsnameId);
            if (null!=byId){
            item.setAdvposname(byId.getAdvposname());
            }
            return item;
        }).collect(Collectors.toList());




        //调用老师封装的类common
        return ResultData.ok(" ", PageResult.init(pageInfo.getTotal(), pageInfo.getSize(), pageInfo.getPages(), pageInfo.getRecords()));
    }

    /**
     * 根据id查询方法
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(@RequestParam Long id) {
        log.info("查询id为:{}", id);
        Advimg advimg = advimgService.getById(id);

        return ResultData.ok("", advimg);
    }

    /**
     * 修改方法
     *
     * @param advimg
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Advimg advimg) {
        log.info("advimg为:{}", advimg);
        advimgService.updateById(advimg);

        return ResultData.ok("", "成功");
    }

    /**
     * 删除方法
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(@RequestParam Long id) {
        log.info("删除id为:{}", id);
        advposService.removeById(id);

        return ResultData.ok("", "成功");
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody Advimg advimg){
        advimg.setDefault_data("1");
        return ResultData.ok("",advimgService.save(advimg));
    }
}
